/*
 *    Copyright (c) 2018-2025, exsecs All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: exsecs
 */

package com.excesys.exsecs.energy.task;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.excesys.exsecs.access.constant.RedisConstant;
import com.excesys.exsecs.access.entity.AccessDeviceInfo;
import com.excesys.exsecs.access.mapper.AccessDeviceInfoMapper;
import com.excesys.exsecs.access.service.AccessDeviceInfoService;
import com.excesys.exsecs.energy.constant.exsecsQuartzEnum;
import com.excesys.exsecs.energy.entity.AmmeterFlow;
import com.excesys.exsecs.energy.entity.BumDevEnroll;
import com.excesys.exsecs.energy.service.AmmeterFlowService;
import com.excesys.exsecs.energy.service.DevEnrollService;
import com.excesys.exsecs.energy.util.DateUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

import static cn.hutool.core.date.DateTime.now;

/**
 * @author 郑健楠
 */
@Slf4j
@Component("ammeter")
public class AmmeterTask {
	@Autowired
	private StringRedisTemplate redisTemplate;
    @Autowired
	private DevEnrollService devEnrollService;
    @Autowired
	private AccessDeviceInfoMapper accessDeviceInfoMapper;
	@Autowired
	private AmmeterFlowService ammeterFlowService;
	/**
	 * 测试Spring Bean的演示方法
	 */
	@SneakyThrows
	public String demoMethod(String para) {
		log.info("测试于:{}，输入参数{}", LocalDateTime.now(), para);
		log.info("开始同步电表数据。。。。。");
		Date currentDate = DateUtil.getSecondPoint(now());
	    List<BumDevEnroll> bumDevEnrollList = 	devEnrollService.list();
		for(BumDevEnroll bumDevEnroll : bumDevEnrollList){
			QueryWrapper<AccessDeviceInfo> queryWrapper = Wrappers.query();
			queryWrapper.eq("unit_no", bumDevEnroll.getUnitNo());
		   AccessDeviceInfo deviceInfo = accessDeviceInfoMapper.selectOne(queryWrapper);
			if(ObjectUtil.isNotEmpty(deviceInfo)){
				if(deviceInfo.getOnlineStatus()==1){
					AmmeterFlow ammeterFlow = new AmmeterFlow();
					//log.info("打印数据deviceInfo："+deviceInfo);
					Object objData = redisTemplate.opsForHash().get(RedisConstant.REPORT_LIST_KEY, deviceInfo.getUnitNo());
					if(ObjectUtil.isNotEmpty(objData)){
						JSONObject dataObject = JSONObject.parseObject(objData.toString());
						if(ObjectUtil.isNotEmpty(dataObject.get("data"))){
							ammeterFlow.setUnitNo(deviceInfo.getUnitNo());
							ammeterFlow.setData(String.valueOf(dataObject.get("data")));
							ammeterFlow.setCreateTime(currentDate);
							ammeterFlow.setTimePartition(Integer.parseInt(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMM"))));
						}
						ammeterFlowService.save(ammeterFlow);;
					}
				}
			}
		}

		log.info("结束同步电表数据。。。。。");
		return exsecsQuartzEnum.JOB_LOG_STATUS_SUCCESS.getType();
	}

}
